package com.citas.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.sql.rowset.CachedRowSet;

import com.sun.rowset.CachedRowSetImpl;

class Abstraccion
{
 	
  private Connection link;
  private Statement stmt; 
  public void connect() 
  {
	  
	  String connectString = "jdbc:postgresql"+"://"+Conf.getInstance().dominio+":5432/"+Conf.getInstance().servidorbase+"";

	
  	  String usuario=Conf.getInstance().servidorusuario;

	  String clave=Conf.getInstance().servidorclave;
	  try {
		Class.forName(Conf.getInstance().driver);
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  try {
		link = DriverManager.getConnection(connectString, usuario , clave);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  
      //String this->link=mysql_connect($server,$usuario,$clave);
      //mysql_select_db($base,$this->link);
      try {
		stmt = link.createStatement();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  
	  
  }
  
  public void disconnect() throws SQLException
  {
	 //mysql_close($this->link);
	 stmt.close();
	 link.close();
  }
  
  public ArrayList<String[]> getDataTable(String cadena) throws SQLException, ClassNotFoundException
  {
	 int y;
	 ArrayList<String[]> tabledata = new ArrayList<String[]>();
	 this.connect();
	 
	 
	 ResultSet tabla=null;
	 CachedRowSet crs = new CachedRowSetImpl();
	 tabla=stmt.executeQuery(cadena);
	 crs.populate(tabla);
	 
	 y=crs.getMetaData().getColumnCount();
	 //tabledata=new String[y];
	 //crs.next();
	 //tabla2=tabla;

	while (crs.next()){
		
		String lo[]=new String[y];
//		 System.out.println(tabla.getMetaData().get);
		for(int u=1;u<y+1;u++){
		lo[u-1]=crs.getString(u);
		}
		
		tabledata.add(lo);

	}
	for(int l=0;l<tabledata.size();l++)
	{}
		
		
	 tabla.close();
	 this.disconnect();
	 return tabledata; 
  
  }
 /* public function getDataOnly($cadena)
  {
	 $this->connect();
	 $valor=mysql_query($cadena,$this->link);
	 $this->disconnect();
	  return $valor;
  }
  
  public function insertDataTable($cadena)
  {
	$this->connect();
	mysql_query($cadena,$this->link);
	$this->disconnect();
	 
  }
  public function updateDataTable($cadena)
  {
	$this->conectar();
	mysql_query($cadena,$this->link);
	$this->desconectar();
	 
  }
  public function deleteDataTable($cadena)
  {
	$this->conectar();
	mysql_query($cadena,$this->link);
	$this->desconectar();
	 
  }
  

  private function __construct()
  {
	  
	  
  }*/

  private static Abstraccion instance;
  public static synchronized Abstraccion getInstance()
  {
	   if(instance==null)
	   {
		instance=new Abstraccion();   
	   }
	   return instance;
	  
  }
  
  public Integer updateDataTable(String cadena) throws SQLException
  {	Integer valor=0;
	  this.connect();
	  valor=stmt.executeUpdate(cadena);
	  //stmt.executeQuery(cadena);
	  this.disconnect();
	  return valor;
  }


}
